package com.example.gbtdemo.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.gbtdemo.entity.AppUser;
import com.example.gbtdemo.mapper.AppUserMapper;
import com.example.gbtdemo.service.AppUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.Future;

/**
 * <p>
 * app用户表 服务实现类
 * </p>
 *
 * @author 刘泽
 * @since 2023-01-13 12:21:05
 */
@Service
@Slf4j
public class AppUserServiceImpl extends ServiceImpl<AppUserMapper, AppUser> implements AppUserService {
    private static final Integer SIZE = 1000;
    private static final Integer DELSIZE = 10000;
    private static final Integer DELSIZE_10 = DELSIZE / 10;
    @Resource
    private AppUserMapper appUserMapper;
    @Resource
    private UseraAppBakAsync useraAppBakAsync;

    @Override
    @Async(value = "taskliuzeExecutor")
    public void aaaa() {
        // 这里很快 !!!
        System.out.println(Thread.currentThread().getName());
        // 这里很快 !!!
    }

    @Override
    public Object getpages(Integer page, Integer size) {
        page = page * size;
        // System.out.println(totalPages(size).toString());
        return appUserMapper.getpages(page, size);
    }

    @Override
    public void async() {
        TimeInterval timer = DateUtil.timer();
        HashMap<String, Long> objectHashMap = totalPages(SIZE);
        Long totalPages = objectHashMap.get("totalPages");
        List<Future<Object>> futureList = new ArrayList<>();
        for (int i = 0; i < totalPages; i++) {
            // 添加操作
            Future<Object> objectFuture = useraAppBakAsync.addUseraAppBak(i, SIZE);
            futureList.add(objectFuture);
        }
        while (true) {
            boolean isAllDone = true;
            for (Future<Object> future : futureList) {
                if (null == future || !future.isDone()) {
                    isAllDone = false;
                }
            }
            if (isAllDone) {
                break;
            }
        }
        log.info("执行备份完毕----准备删除");
        HashMap<String, Long> DELobjectHashMap = totalPages(DELSIZE);
        Long DELtotalPages = DELobjectHashMap.get("totalPages");
        for (int i = 0; i < DELtotalPages; i++) {
            List<String> ids = appUserMapper.selectLimitId(DELSIZE);
            List<List<String>> partition = Lists.partition(ids, DELSIZE_10);
            List<Future<Object>> deletefutureList = new ArrayList<>();
            for (int j = 0; j < partition.size(); j++) {
                Future<Object> deleobjectFuture = useraAppBakAsync.deleteuab(i, partition.get(j));
                deletefutureList.add(deleobjectFuture);
            }
            while (true) {
                boolean isAllDone = true;
                for (Future<Object> future : deletefutureList) {
                    if (null == future || !future.isDone()) {
                        isAllDone = false;
                    }
                }
                if (isAllDone) {
                    break;
                }
            }
        }
        log.info("执行删除完毕----执行时间(秒):{}", timer.intervalSecond());
        log.info("执行删除完毕----执行时间(分钟):{}", timer.intervalMinute());
    }


    public HashMap<String, Long> totalPages(Integer size) {
        Long selectCount = this.baseMapper.selectCount(new QueryWrapper<>());
        HashMap<String, Long> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("totalPages", (selectCount - 1) / size + 1);
        stringObjectHashMap.put("total", selectCount);
        return stringObjectHashMap;
    }
}
